<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>商品管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/layui/css/layui.css"  media="all">
  <script src="/js/jquery-1.12.3.min.js" charset="utf-8"></script>
  <script src="/layui/layui.js" charset="utf-8"></script>

</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>
 
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    <button class="layui-btn layui-btn-sm" lay-event="batchdel">批量删除</button>
  </div>
</script>
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
layui.use('table', function(){
  var table = layui.table;
  
  table.render({
    elem: '#test'
    ,url:'{:url("goods/lists")}'
    ,toolbar: '#toolbarDemo'
    ,title: '用户数据表'
    ,totalRow: false
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field:'ID', title:'ID', width:50, fixed: 'left', unresize: true,type:'numbers'}
      ,{field:'pno', title:'商品编号', width:100}
      ,{field:'name', title:'商品名称', width:130}
      ,{field:'type', title:'型号', width:80}
      ,{field:'brand', title:'品牌', width:80}
      ,{field:'color', title:'颜色', width:80}
      ,{field:'gunge1', title:'规格1', width:100}
      ,{field:'gunge2', title:'规格2', width:100}
      ,{field:'introduce', title:'介绍', width:120}
      ,{field:'pic', title:'图片', width:120}
      ,{field:'uptime', title:'上市时间', width:120}
      ,{field:'price0', title:'进价', width:80}
      ,{field:'price1', title:'市场价', width:80}
      ,{field:'price2', title:'优惠价', width:80}
      ,{field:'num', title:'库存', width:60}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:120}
    ]]
    ,page: true
    ,parseData: function(res){ 
      return {
        "code": 0, 
        "msg": "", 
        "count": res.length, 
        "data": res
      };
    }
    
  });
  
   

  //工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'add':
        layer.open({
          type: 2,
          title: '添加',
          skin: 'layui-layer-rim', //加上边框
          area: ['450px', '520px'], //宽高
          content: '{:url("goods/add")}'
        })

      break;

      case 'batchdel':
        var data = checkStatus.data;
            delList=[];       
            for (var i = 0; i < data.length; i++) {
                delList.push(data[i].id);
             }
        var ids = JSON.stringify(delList);
         layer.confirm('真的全部删除么', function(index){
            $.ajax({
                type:"post",
                url:"{:url('goods/batchdel')}",
                dataType:"json",
                data:{ids:ids},
                success:function(data){             
                    layer.msg(data, {
                    icon: 6,//成功的表情
                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                }, function(){
                    location.reload();
                });
                }
            }); 
           layer.close(index);
        });
      break;


    };
  });

  //监听行工具事件
  table.on('tool(test)', function(obj){
    var data = obj.data;
    console.log(data.id)
    if(obj.event === 'del'){
      layer.confirm('真的删除行么', function(index){
            $.ajax({
                type:"get",
                url:"{:url('goods/del')}",
                dataType:"text",
                data:{id:data.id},
                success:function(data){             
                    layer.msg(data, {
                    icon: 6,//成功的表情
                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                }, function(){
                    location.reload();
                });
                }
            }); 
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
        layer.open({
          type: 2,
          title: '修改',
          skin: 'layui-layer-rim', //加上边框
          area: ['450px', '460px'], //宽高
          content: '{:url("goods/edit")}',
          success: function(layero, index){
              var body = layer.getChildFrame('body', index);
              var iframeWin = layero.find('iframe')[0].contentWindow;
              body.find('#id').val(data.id)
              body.find('#no').val(data.no)
              body.find('#name').val(data.name)
              body.find("input[name=sex][value=0]").attr("checked",data.sex == 0 ? true : false);
              body.find("input[name=sex][value=1]").attr("checked",data.sex == 1 ? true : false);
              body.find('#phone').val(data.phone)
              body.find('#address1').val(data.address1)
              body.find('#address2').val(data.address2)
              body.find('#zhiwu').val(data.zhiwu)
              body.find('#password').val(data.password)
              body.find('#jobtime').val(data.jobtime)
              body.find('#xueli').val(data.xueli)
              body.find("input[name=xueli][value=0]").attr("checked",data.xueli == 0 ? true : false);
              body.find("input[name=xueli][value=1]").attr("checked",data.xueli == 1 ? true : false);
              body.find("input[name=xueli][value=2]").attr("checked",data.xueli == 2 ? true : false);
              body.find("input[name=xueli][value=3]").attr("checked",data.xueli == 3 ? true : false);
              body.find("input[name=xueli][value=4]").attr("checked",data.xueli == 4 ? true : false);
              body.find("input[name=xueli][value=5]").attr("checked",data.xueli == 5 ? true : false);
              body.find("input[name=xueli][value=6]").attr("checked",data.xueli == 6 ? true : false);
              body.find('#major').val(data.major)
              body.find('#gradetime').val(data.gradetime)
              body.find("input[name=zhicheng][value=0]").attr("checked",data.zhicheng == 0 ? true : false);
              body.find("input[name=zhicheng][value=1]").attr("checked",data.zhicheng == 1 ? true : false);
              body.find("input[name=zhicheng][value=2]").attr("checked",data.zhicheng == 2 ? true : false);
              body.find("input[name=zhicheng][value=3]").attr("checked",data.zhicheng == 3 ? true : false);
              body.find('#introduce').val(data.introduce)
              iframeWin.layui.form.render();
            },
          cancel: function(index, layero){     
              location.reload(); 
            } 

        }) 
    }
  });

});

</script>


</body>
</html>